import { db } from '../index';
import { systemLogs } from '../schema';
import { eq } from 'drizzle-orm';
import { createId } from '@paralleldrive/cuid2';

/**
 * 系统日志种子数据
 */
export async function runSystemLogSeeds() {
  try {
    console.log('🔄 创建系统日志种子数据...');
    
    // 检查是否已存在日志数据
    const existingLogs = await db.select().from(systemLogs).limit(1);
    if (existingLogs.length > 0) {
      console.log('⏭️  系统日志数据已存在，跳过创建');
      return;
    }

    // 创建示例日志数据
    const logData = [
      {
        id: createId(),
        level: 'info' as const,
        module: 'auth',
        action: 'login',
        message: '用户登录成功',
        userId: 'admin',
        username: 'admin',
        ip: '127.0.0.1',
        userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
        createdAt: new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(), // 1天前
        updatedAt: new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString()
      },
      {
        id: createId(),
        level: 'info' as const,
        module: 'case',
        action: 'create',
        message: '创建案例：星巴克门店标识系统',
        userId: 'admin',
        username: 'admin',
        ip: '127.0.0.1',
        userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
        createdAt: new Date(Date.now() - 20 * 60 * 60 * 1000).toISOString(), // 20小时前
        updatedAt: new Date(Date.now() - 20 * 60 * 60 * 1000).toISOString()
      },
      {
        id: createId(),
        level: 'warn' as const,
        module: 'contact',
        action: 'update',
        message: '联系人信息更新：缺少必要字段',
        userId: 'admin',
        username: 'admin',
        ip: '127.0.0.1',
        userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
        createdAt: new Date(Date.now() - 18 * 60 * 60 * 1000).toISOString(), // 18小时前
        updatedAt: new Date(Date.now() - 18 * 60 * 60 * 1000).toISOString()
      },
      {
        id: createId(),
        level: 'info' as const,
        module: 'product',
        action: 'view',
        message: '查看产品列表',
        userId: 'admin',
        username: 'admin',
        ip: '127.0.0.1',
        userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
        createdAt: new Date(Date.now() - 12 * 60 * 60 * 1000).toISOString(), // 12小时前
        updatedAt: new Date(Date.now() - 12 * 60 * 60 * 1000).toISOString()
      },
      {
        id: createId(),
        level: 'error' as const,
        module: 'system',
        action: 'backup',
        message: '数据库备份失败：磁盘空间不足',
        userId: 'system',
        username: 'system',
        ip: '127.0.0.1',
        userAgent: 'System Process',
        createdAt: new Date(Date.now() - 8 * 60 * 60 * 1000).toISOString(), // 8小时前
        updatedAt: new Date(Date.now() - 8 * 60 * 60 * 1000).toISOString()
      },
      {
        id: createId(),
        level: 'info' as const,
        module: 'auth',
        action: 'logout',
        message: '用户退出登录',
        userId: 'admin',
        username: 'admin',
        ip: '127.0.0.1',
        userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
        createdAt: new Date(Date.now() - 4 * 60 * 60 * 1000).toISOString(), // 4小时前
        updatedAt: new Date(Date.now() - 4 * 60 * 60 * 1000).toISOString()
      },
      {
        id: createId(),
        level: 'info' as const,
        module: 'case',
        action: 'update',
        message: '更新案例：中山市人民医院导视系统',
        userId: 'admin',
        username: 'admin',
        ip: '127.0.0.1',
        userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
        createdAt: new Date(Date.now() - 2 * 60 * 60 * 1000).toISOString(), // 2小时前
        updatedAt: new Date(Date.now() - 2 * 60 * 60 * 1000).toISOString()
      },
      {
        id: createId(),
        level: 'debug' as const,
        module: 'api',
        action: 'request',
        message: 'API请求：GET /api/cases',
        userId: null,
        username: null,
        ip: '127.0.0.1',
        userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
        createdAt: new Date(Date.now() - 1 * 60 * 60 * 1000).toISOString(), // 1小时前
        updatedAt: new Date(Date.now() - 1 * 60 * 60 * 1000).toISOString()
      },
      {
        id: createId(),
        level: 'info' as const,
        module: 'system',
        action: 'startup',
        message: '系统启动完成',
        userId: 'system',
        username: 'system',
        ip: '127.0.0.1',
        userAgent: 'System Process',
        createdAt: new Date(Date.now() - 30 * 60 * 1000).toISOString(), // 30分钟前
        updatedAt: new Date(Date.now() - 30 * 60 * 1000).toISOString()
      },
      {
        id: createId(),
        level: 'info' as const,
        module: 'contact',
        action: 'create',
        message: '创建新联系人',
        userId: 'admin',
        username: 'admin',
        ip: '127.0.0.1',
        userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
        createdAt: new Date(Date.now() - 10 * 60 * 1000).toISOString(), // 10分钟前
        updatedAt: new Date(Date.now() - 10 * 60 * 1000).toISOString()
      }
    ];

    // 插入日志数据
    await db.insert(systemLogs).values(logData);
    
    console.log(`✅ 成功创建 ${logData.length} 条系统日志记录`);
    
  } catch (error) {
    console.error('❌ 创建系统日志种子数据失败:', error);
    throw error;
  }
}